﻿namespace ManagerClinic.TiepNhan
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmDanhSachBenhNhan : DevExpress.XtraEditors.XtraForm
    {
        DmBenhNhanBusiness dmBenhNhanBusiness = new DmBenhNhanBusiness();
        IList<DmBenhNhanEntity> lstDmBenhNhanEntity = null;
        GridCheckMarksSelection gridCheckMarksSelection = null;
        Form parentForm = null;

        void LoadDataGrid(object sender, EventArgs e)
        {
            lstDmBenhNhanEntity = dmBenhNhanBusiness.GetDmBenhNhanList();
            grdBenhNhan.DataSource = lstDmBenhNhanEntity;
        }

        public frmDanhSachBenhNhan(Form frm)
        {
            InitializeComponent();

            parentForm = frm;
            gridCheckMarksSelection = new GridCheckMarksSelection(gvBenhNhan);
        }

        private void frmDanhSachBenhNhan_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadDataGrid(sender, e);
        }

        DmBenhNhanEntity getDataDmBenhNhanEntity()
        {
            if (gvBenhNhan.RowCount > 0)
            {
                return (DmBenhNhanEntity)gvBenhNhan.GetFocusedRow();
            }

            return null;
        }

        private void grdBenhNhan_DoubleClick(object sender, EventArgs e)
        {
            btnSua_Click(sender, e);
        }

        private void gvBenhNhan_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmBenhNhanEntity dm in deleteObjects)
            {
                dmBenhNhanBusiness.DeleteDmBenhNhan(dm.MaBenhNhan);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadDataGrid(sender, e);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var dmBenhNhanEntity = getDataDmBenhNhanEntity();
            if (dmBenhNhanEntity == null) return;

            var frm = new frmCapNhatDmBenhNhan((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmBenhNhanEntity, parentForm);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmBenhNhan((int)ConstantManagerClinic.StateInsertOrUpdate.Insert, parentForm);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }
    }
}